package com.hskn.hss.module.role.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.core.utils.spring.SpringUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.rectifyrecord.entity.RectifyRecord;
import com.hskn.hss.module.rectifyrecord.mapper.RectifyRecordMapper;
import com.hskn.hss.module.role.entity.SysRole;
import com.hskn.hss.module.role.mapper.SysRoleMapper;
import com.hskn.hss.module.role.service.SysRoleService;
import com.hskn.hss.module.userrole.service.impl.SysUserRoleServiceImpl;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2020-11-16
 */
@Api(tags = {"-role"}, description = "“角色管理")
@Slf4j
@RestController
@RequestMapping("/hss/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /***
     * 新增角色信息
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.INSERT, tableName = "sys_role", bean = SysRole.class, beanMapper = SysRoleMapper.class, names = "roleName")
    @RequestMapping(value = "insertrole", method = RequestMethod.POST)
    public AjaxResult<Object> insertrole(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysRoleService.insertRole(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    /**
     * 修改角色信息
     *
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_role", idKey = "id", bean = SysRole.class, beanMapper = SysRoleMapper.class, names = "roleName")
    @RequestMapping(value = "updaterole", method = RequestMethod.POST)
    public AjaxResult updaterole(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysRoleService.updateRole(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    /***
     * 删除角色信息
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.DELETE, tableName = "sys_role", idKey = "id", bean = SysRole.class, beanMapper = SysRoleMapper.class, names = "roleName")
    @RequestMapping(value = "deleteroleByid", method = RequestMethod.POST)
    public AjaxResult<Object> deleteroleByid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            String id = "";
            if(params.containsKey("id")){
                id = params.getString("id");
            }
            if(CollectionUtils.isNotEmpty(SpringUtils.getBean(SysUserRoleServiceImpl.class).getSysUserRoleListByRoleId(id))){
                return AjaxResult.fail("该角色已分配用户，删除失败！");
            }
            return sysRoleService.deleteroleByid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "sys_role", idKey = "ids", bean = SysRole.class, beanMapper = SysRoleMapper.class, names = "roleName")
    @ApiOperation(value = "批量删除接口", notes = "批量删除")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = SysRole.class)
    })
    @RequestMapping(value = "deletebatchbyid", method = RequestMethod.POST)
    public AjaxResult<Object> deletebatchbyid(@RequestBody @Validated SysRole sysRole) {

        try {
            return sysRoleService.deletebatchbyid(sysRole);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    /**
     * 根据Id获取角色信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getrolebyid", method = RequestMethod.POST)
    public AjaxResult<Object> getrolebyid(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        SysRole sysRole = null;
        try {
            sysRole = sysRoleService.getRoleById(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(sysRole);

    }

    /**
     * 获取角色列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getrolelist", method = RequestMethod.POST)
    public AjaxResult<Object> getrolelist(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            JSONObject json = sysRoleService.getRoleList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }

    }
    /**
     * 选择权限查询，不是超管不返回超管
     * @return
     */
    @PostMapping(value = "selectRoleList")
    public AjaxResult selectRoleList() {
        try {
            return AjaxResult.success(sysRoleService.selectRoleList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }

    }

    /***
     * 角色状态修改
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_role", idKey = "ids", bean = SysRole.class, beanMapper = SysRoleMapper.class, names = "roleName")
    @ApiOperation(value = "批量停用接口", notes = "批量停用")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = SysRole.class)
    })
    @RequestMapping(value = "changeStatus", method = RequestMethod.POST)
    public AjaxResult<Object> changeStatus(@RequestBody @Validated SysRole sysRole,HttpServletRequest request) {

        try {
            return sysRoleService.changeStatus(sysRole);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("修改失败，请稍后再试");
        }
    }

}

